package com.lxl.bt.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.lxl.bt.R;
import com.lxl.bt.adapter.BTCFragmentAdapter;
import com.lxl.bt.bean.UserAccountBean;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.ACache;
import com.lxl.bt.view.CustomCommonDialog;
import com.lxl.bt.view.CustomViewPager;
import com.yanzhenjie.sofia.Sofia;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by lvxinlong
 * on  BTC钱包
 * on  2021年3月6日09:45:28
 **/
public class BTCActivity extends BaseActivity {


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tvZiChan)
    TextView tvZiChan;
    @BindView(R.id.tv_freeze)
    TextView tvFreeze;
    @BindView(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.view_pager)
    CustomViewPager viewPager;
    @BindView(R.id.tvCz)
    TextView tvCz;
    @BindView(R.id.tvTx)
    TextView tvTx;
    private Context context;
    ACache aCache;
    BTCFragmentAdapter selectionCenterFragmentAdapter;
    private int current;
    UserAccountBean userAccountBean;
    private final String[] titles = {"全部", "挖矿","冻结", "充值", "提现", "支付"};
    /* 收益类型 */
//    DEALER_ADD("1", "挖矿收益"),
//    DEALER_PROXY("2", "佣金收益"),
//    DEALER_SUB("3", "提现"),
//    DEALER_RECH("4", "充值"),
//    DEALER_PAY("5", "支付");
    private final String[] incomeTypes = {"", "1","1", "4", "3", "5"};
    private final String[] activityTitles = {"USDT钱包", "BTC钱包", "ETH钱包", "LTC钱包", "FIL钱包", "电费钱包"};
    private String[] reqs = {"usdt", "btc", "eth", "ltc", "fil", ""};
    private int name;
    private String coinName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_btc);
        ButterKnife.bind(this);
        //状态栏全透明全屏显示页面，布局能够延伸到状态栏和导航栏下方
        Sofia.with(this)
                .invasionStatusBar()
                .statusBarLightFont()
//                .statusBarDarkFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);

        context = this;
        rlBack.setVisibility(View.VISIBLE);
        userAccountBean = (UserAccountBean) getIntent().getSerializableExtra("data");
        name = getIntent().getIntExtra("name", 0);
        coinName = getIntent().getStringExtra("coinName");
        tvRight.setText("复投");
        rlRight.setVisibility(View.GONE);
        txtToolbarTitle.setText(activityTitles[name]);

        switch (name) {
            case 0:
                tvZiChan.setText(userAccountBean.getUsdtAmount() + "≈ ¥" + userAccountBean.getUsdtAmountPrice());
                tvFreeze.setText(userAccountBean.getUsdtFreeze());
                break;
            case 1:
                tvZiChan.setText(userAccountBean.getBtcAmount() + "≈ ¥" + userAccountBean.getBtcAmountPrice());
                tvFreeze.setText(userAccountBean.getBtcFreeze());
                break;
            case 2:
                tvZiChan.setText(userAccountBean.getEthAmount() + "≈ ¥" + userAccountBean.getEthAmountPrice());
                tvFreeze.setText(userAccountBean.getEthFreeze());
                break;
            case 3:
                tvZiChan.setText(userAccountBean.getLtcAmount() + "≈ ¥" + userAccountBean.getLtcAmountPrice());
                tvFreeze.setText(userAccountBean.getLtcFreeze());
                break;
            case 4:
                tvZiChan.setText(userAccountBean.getFilAmount() + "≈ ¥" + userAccountBean.getFilAmountPrice());
                tvFreeze.setText(userAccountBean.getFilFreeze());
                break;
            case 5:
                tvZiChan.setText("0");
                tvFreeze.setText("0");
                break;
        }
        aCache = ACache.get(this);
        refFragmentView();
    }

    void refFragmentView() {
        selectionCenterFragmentAdapter = new BTCFragmentAdapter(getSupportFragmentManager(), titles, incomeTypes, coinName);
        viewPager.setAdapter(selectionCenterFragmentAdapter);
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(current);
        viewPager.setOffscreenPageLimit(titles.length);

        refTableTitleView();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                current = position;
                refTableTitleView();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void isValid(int type) {
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .isValid().enqueue(new NetCallback<Result<Integer>>() {
            @Override
            public void onSuccess(Result<Integer> result) {
                if (result.isSuccess()) {
                    //是否认证0：未认证 1认证
                    if (result.getData() == 0) {
                        new CustomCommonDialog.Builder(BTCActivity.this, false, true, true, Gravity.CENTER)
                                .shiminDialog().show();
                    } else {
                        if (type == 1) {
                            startActivity(new Intent(BTCActivity.this, ChongzhiActivity.class).putExtra("data", userAccountBean).putExtra("name", name));
                        } else {
                            startActivity(new Intent(BTCActivity.this, TibiActivity.class).putExtra("data", userAccountBean).putExtra("name", name));
                        }

                    }
                }
            }

            @Override
            public void netLogin() {

            }

            @Override
            public void onError(String errorStr) {
                super.onError(errorStr);
                Log.w("lxl", errorStr);

            }

        });
    }

    private void refTableTitleView() {
        for (int i = 0; i < titles.length; i++) {
            if (i == current) {
                tabLayout.getTitleView(current).getPaint().setFakeBoldText(true);
            } else {
                tabLayout.getTitleView(i).getPaint().setFakeBoldText(false);
            }
        }
    }

    @OnClick({R.id.rlBack, R.id.tvCz, R.id.tvTx})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlBack:
                finish();
                break;
            case R.id.tvCz:
                isValid(1);
                break;
            case R.id.tvTx:
                isValid(2);
                break;
        }
    }
}
